/**
 * 下载文件
 * @param {string} url - 下载文件链接
 */
export const downloadFile = (url) => {
  let iframe = document.querySelector('#downloadFileIframe')
  iframe && document.body.removeChild(iframe)

  iframe = document.createElement('iframe')
  iframe.id = 'downloadFileIframe'
  iframe.src = url
  iframe.getElementsByClassName.display = 'none'
  document.body.appendChild(iframe)
}

export const downLoadByBlob = response => {
  let fileName = response.headers['content-disposition'].match(/filename=(\S*)xls/)[0]
  fileName = fileName.split('=')[1]

  const blob = new Blob([response.data], { type: response.headers['content-type'] })

  const aLink = document.createElement('a')
  aLink.href = URL.createObjectURL(blob)
  aLink.setAttribute('download', fileName)
  aLink.style.display = 'none'
  document.body.appendChild(aLink)
  aLink.click()
  document.body.removeChild(aLink)
}
